package com.ruoyi.web.controller.bus;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.utils.DeptUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.bus.domain.WitAppoint;
import com.ruoyi.bus.service.IWitAppointService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆预约Controller
 * 
 * @author caiyanshan
 * @date 2021-04-14
 */
@RestController
@RequestMapping("/bus/appoint")
public class WitAppointController extends BaseController
{
    @Autowired
    private IWitAppointService witAppointService;

    /**
     * 查询车辆预约列表
     */
    @PreAuthorize("@ss.hasPermi('bus:appoint:list')")
    @GetMapping("/list")
    public TableDataInfo list(WitAppoint witAppoint)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if(!SecurityUtils.isAdmin(user.getUserId())){
            witAppoint.setDepts(DeptUtils.getInstitutionIdsBySysUser(user));
        }
        startPage();
        List<WitAppoint> list = witAppointService.selectWitAppointList(witAppoint);
        return getDataTable(list);
    }

    /**
     * 导出车辆预约列表
     */
    @PreAuthorize("@ss.hasPermi('bus:appoint:export')")
    @Log(title = "车辆预约", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(WitAppoint witAppoint)
    {
        List<WitAppoint> list = witAppointService.selectWitAppointList(witAppoint);
        for(WitAppoint appoint : list){
            if(appoint.getDriveTime().equals("am")){
                appoint.setDriveTime("上班");
                continue;
            }
            if(appoint.getDriveTime().equals("pm")){
                appoint.setDriveTime("下班");
            }
        }
        ExcelUtil<WitAppoint> util = new ExcelUtil<WitAppoint>(WitAppoint.class);
        return util.exportExcel(list, "appoint");
    }

    /**
     * 获取车辆预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('bus:appoint:query')")
    @GetMapping(value = "/{appointId}")
    public AjaxResult getInfo(@PathVariable("appointId") Long appointId)
    {
        return AjaxResult.success(witAppointService.selectWitAppointById(appointId));
    }

    /**
     * 新增车辆预约
     */
    @PreAuthorize("@ss.hasPermi('bus:appoint:add')")
    @Log(title = "车辆预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WitAppoint witAppoint)
    {
        return toAjax(witAppointService.insertWitAppoint(witAppoint));
    }

    /**
     * 修改车辆预约
     */
    @PreAuthorize("@ss.hasPermi('bus:appoint:edit')")
    @Log(title = "车辆预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WitAppoint witAppoint)
    {
        return toAjax(witAppointService.updateWitAppoint(witAppoint));
    }

    /**
     * 删除车辆预约
     */
    @PreAuthorize("@ss.hasPermi('bus:appoint:remove')")
    @Log(title = "车辆预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{appointIds}")
    public AjaxResult remove(@PathVariable Long[] appointIds)
    {
        return toAjax(witAppointService.deleteWitAppointByIds(appointIds));
    }
}
